package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Bill;
import school.domain.Tc;
import school.domain.Tclog;
import school.domain.User;
import school.mapper.BillMapper;
import school.mapper.TcMapper;
import school.mapper.TclogMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.pojo.UserPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TcMapper tcMapper;
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private TclogMapper tclogMapper;

    /**
     * 用户列表
     *
     * @param user
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(UserPo user, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("phone", "admin");
        if (user.getName() != null) {
            criteria.andCondition("u.name like ", "%" + user.getName() + "%");
        }
        if (user.getPhone() != null) {
            criteria.andCondition("phone like ", "%" + user.getPhone() + "%");
        }
        if(user.getTcName() != null)  criteria.andCondition("t.name like ", "%" + user.getTcName() + "%");
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<UserPo> pageInfo = new PageInfo<>(userMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "user/list";
    }


    /**
     * 用户详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        User user = userMapper.selectByPrimaryKey(id);
        model.addAttribute(user);
        return "user/detail";
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @RequestMapping("update")
    public String update(User user) {
        if (user.getPassword() == null || user.getPassword().isEmpty()) user.setPassword(null);
        userMapper.updateByPrimaryKeySelective(user);
        return refresh();
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        userMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    @GetMapping("add")
    public String add() {
        return "user/add";
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("add")
    public String add(User user) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Random random = new Random();
        Date date = new Date();
        List<Tc> tcList = tcMapper.selectAll();


        //当前套餐

        Tc tc = tcList.get(random.nextInt(tcList.size()));
        int tcId = tc.getId();
        user.setSms(tc.getSms());
        user.setTraffic(tc.getTraffic());
        user.setVoice(tc.getMinut());
        user.setAddtime(date);
        user.setAccount(1000 - tc.getMoney());
        user.setTcid(tc.getId());

        userMapper.insertSelective(user);

        //账单更换
        Tclog tclog = new Tclog();
        tclog.setAddtime(date);
        tclog.setTcid(tc.getId());
        tclog.setUserid(user.getId());
        tclogMapper.insert(tclog);

        //初始化账单信息
        for (int i = 1; i <= 100; i++) {
            date.setMonth(date.getMonth() - 1);
            tc = tcList.get(random.nextInt(tcList.size()));
            //账单
            Bill bill = new Bill();
            bill.setAddtime(date);
            bill.setTcid(tc.getId());
            bill.setUserid(user.getId());
            bill.setSms(random.nextInt(100));
            bill.setVoice(random.nextInt(100));
            bill.setTraffic(random.nextInt(3000));
            if (bill.getSms() > tc.getSms()) {
                bill.setSmsm((bill.getSms() - tc.getSms()) * tc.getSmstc());
            } else {
                bill.setSmsm(0D);
            }
            if (bill.getTraffic() > tc.getTraffic()) {
                bill.setTrafficm((bill.getTraffic() - tc.getTraffic()) * tc.getTraffictc());

            }else{
                bill.setTrafficm(0D);
            }
            if (bill.getVoice() > tc.getMinut()) {
                bill.setVoicem((bill.getVoice() - tc.getMinut()) * tc.getVoice());
            } else {
                bill.setVoicem(0D);
            }
            bill.setOtherm(random.nextInt(20)+0D);
            bill.setTotal(bill.getOtherm() + bill.getSmsm() + bill.getTrafficm() + bill.getVoicem()+tc.getMoney());
            billMapper.insert(bill);

            if(tcId != tc.getId()){
                tcId = tc.getId();
                tclog = new Tclog();
                tclog.setAddtime(date);
                tclog.setTcid(tc.getId());
                tclog.setUserid(user.getId());
                tclogMapper.insert(tclog);
            }
        }


        return "redirect:list";
    }


    public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Random random = new Random();
        Date date = new Date();
        for (int i = 0; i < 100; i++) {
            date.setMonth(date.getMonth() - 1);
            System.out.println(dateFormat.format(date));
        }

    }
}
